<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <title><!--{$lang.page.index.title}--></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/lay/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/lay/layui/index.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-text-item" style="text-align: center;"><b><!--{$lang.page.table.view}--> <!--{$lang.page.public.password}--></b></div>
     <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <input type="hidden" name="type" value='showpwd'>
        <input type="hidden" name="aid" value='<!--{$aid}-->'>
        <!--{if $spwd.use > 1}--> 
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label"><!--{$lang.page.public.inputcode}--></label>
            <div class="layui-input-inline">
              <input type="text" name="code" lay-verify="code" placeholder="<!--{$lang.page.public.pinput}-->" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button type="button" class="layui-btn" id="sendCode" ><!--{$lang.page.public.send}--></button>
            <div class="layui-btn" id="timer" style="display: none"><!--{$lang.page.public.wait}--></div>
            <button type="button" class="layui-btn" id="submit" lay-submit lay-filter="submit"><!--{$lang.page.public.submit}--></button>
          </div>
          <div class="layui-inline">
            <!--{$lang.page.public.showpwd1}-->
          </div>
        </div>
        <!--{else}-->
        <div class="layui-form-item">
          <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" id="submit" lay-submit lay-filter="submit"><!--{$lang.page.table.view}--></button>
          </div>
          <div class="layui-inline">
            <!--{$lang.page.public.showpwd2}-->
          </div>
        </div>
        <!--{/if}-->
      </div>
      <div class="layui-card-body" id="content"></div> 
     </div>
 </div>
<script src="/static/lay/layui/layui.js"></script>
  <script type="text/javascript">
    layui.config({
     base: '/static/lay/' //静态资源所在路径
   }).extend({
     xadmin: 'lib/xadmin' //弹窗
   }).use(['jquery', 'layer','xadmin', 'form','upload'], function(){
    var form = layui.form
        ,$ = layui.jquery
        ,upload = layui.upload;
     form.render(null, 'component-form-group');
    /* 自定义验证规则 */
    form.verify({
      code: function(value){
        if(value.length < 1){
          return '<!--{$lang.page.public.inputcode}--> <!--{$lang.page.public.nocontent}-->';
        }
        if(value.length >4){
          return '<!--{$lang.page.public.paraerror}-->';
        }
        if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
        {
            return '<!--{$lang.page.public.paraerror}-->';
        }
      }
    });

    form.on('submit(submit)', function (data) {
        $('#submit').attr("disabled",true);
        $('#submit').addClass('layui-btn-disabled');
        $.post('#',data.field,function(res){
            if(res.code == 200){
                html = '';
                if(res.data.password){
                    html += '<div class="layui-form-item"><label class="layui-form-label"><!--{$lang.page.public.loginpwd}-->:'+(res.data.password==1?"<!--{$lang.page.public.setpwd}-->":"")+'</label><button type="button" style="margin:8" class="layui-btn layui-btn-xs copycode" data-set="'+(res.data.password==1?"<!--{$lang.page.public.setpwd}-->":res.data.password)+'"><!--{$lang.page.public.copy}--></button></div>';
                }
                if(res.data.paypwd){
                    html += '<div class="layui-form-item"><label class="layui-form-label"><!--{$lang.page.public.paypwd}-->:'+(res.data.paypwd==1?"<!--{$lang.page.public.setpwd}-->":"")+'</label><button type="button" style="margin:8" class="layui-btn layui-btn-xs copycode" data-set="'+(res.data.paypwd==1?"<!--{$lang.page.public.setpwd}-->":res.data.paypwd)+'"><!--{$lang.page.public.copy}--></button></div>';
                }
                $('#content').html(html);
                layer.msg(res.msg);
            }else{
                layer.msg(res.msg);
            }
        })
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    $('#sendCode').click(function(){
        $('#sendCode').attr("disabled",true);
        $('#sendCode').addClass('layui-btn-disabled');
        $('#submit').removeAttr("disabled");
        $('#submit').removeClass('layui-btn-disabled');
        $.post('sendmail',{type:'showpwd',aid:'<!--{$aid}-->'},function(res){
                if(res.code == 200){
                  $('#sendCode').hide();
                  $('#timer').show();
                  timer(res.data.endtime);
                  layer.msg(res.msg);
                }else{
                  layer.msg(res.msg);
                }
              })
    });

    $(document).on('click', '.copycode', function(e) {
        var data = e.target.dataset.set;
        var oInput = document.createElement('input');
        oInput.value = data;
        document.body.appendChild(oInput);
        oInput.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        oInput.className = 'oInput';//设置class名
        document.getElementsByClassName("oInput")[0].remove();//移除这个input
        layer.msg('<!--{$lang.page.public.copysuc}-->', { icon: 0, skin: 'success_msg', time: 3000 });// success_msg; error_msg;warn_msg; 
    })

  });
  function timer(time){
        var endtime =  time;
        if(endtime>0){
          let setInt=setInterval(() => {
              var countdownElement = document.getElementById("timer");
              var now = new Date().getTime(); // 当前时间
              var distance = Math.floor((endtime*1000 - now)/1000); // 距离结束日期还有多少时间
              if(distance<0){
                clearInterval(setInt); // 如果已经到达或超过了结束日期，清除定时器并隐藏倒计时元素
                countdownElement.style.display = "none";
                $('#sendCode').show();
                $('#sendCode').removeAttr("disabled");
                $('#sendCode').removeClass('layui-btn-disabled');
                return;
              }else{
                showtime = distance>0?distance:0;
                countdownElement.innerText = "" + showtime + "s"; // 更新倒计时内容
                return;
              }
          },1000)
        }else{
          countdownElement.style.display = "none";
          $('#sendCode').show();
      }
  }
  </script>
</body>
</html>

